package research.ui.editors;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.widgets.Section;
import org.eclipse.ui.forms.widgets.TableWrapData;

import research.domain.ComponentValue;
import research.domain.Formula;
import research.entity.Entity;
import research.entity.EntityType;
import research.model.FormulaHierarchy;
import research.persistence.PersistenceManager;
import research.ui.editors.input.EntityEditorInput;
import research.ui.editors.tableeditors.formula.FormulaTableEditor;

public class FormulaEditor extends FormEntityEditor {
	public static String ID = "research.ui.editors.FormulaEditor";
	private Text name;
	private Text description;
	private FormulaTableEditor valuesEditor;
	private List<Entity> values;
	private List<Entity> initValues;
	
	private Formula getEntity() {
		return (Formula) entity;
	}

	@Override
	protected boolean checkInputEntityType(EntityEditorInput input) {
		return input.getEntity().getType().equals(EntityType.Formula);
	}

	@Override
	protected void fill() {
		this.name.setText(this.getEntity().getName());
		this.description.setText(this.getEntity().getDescription());
		
		this.valuesEditor.setInput(new ArrayList<Entity>(values));
		valuesEditor.addModifyListener(getEntityTableListener());
	}

	@Override
	protected void initialize() {
		if (!isNewEntity()) {
			values = PersistenceManager.getInstance().getDataSource().getChildren(getEntity(), new FormulaHierarchy());
		} else {
			values = new ArrayList<Entity>();
		}
		
		initValues = new ArrayList<Entity>(values);
	}
	
	@Override
	protected void persist() {
		
		for (Entity oldpar : initValues) {
			if (!valuesEditor.getEntityList().getEntities().contains(oldpar)) {
				getPersistenceManager().remove(oldpar);
			}
		}

		for (Entity par : valuesEditor.getEntityList().getEntities()) {
			ComponentValue val = (ComponentValue) par;
			if (val.getComponent() != null) {
				val.setFormula(getEntity());
				getPersistenceManager().persist(val);
			}
		}
	}

	@Override
	protected boolean readAndValidate() {
		this.getEntity().setName(this.name.getText());
		this.getEntity().setDescription(this.description.getText());
		
		boolean hasMain = false;
		for (Entity par : valuesEditor.getEntityList().getEntities()) {
			ComponentValue val = (ComponentValue) par;
			if (val.isIs_main()) {
				if (hasMain) {
					MessageDialog.openWarning(getSite().getShell(), "Error", "Задано более одного основного компонента в рецептуре");
					return false;
				}
				hasMain = true;
			}
		}
		if (!hasMain) {
			MessageDialog.openWarning(getSite().getShell(), "Error", "В рецептуре не указан основной компонент");
			return false;
		}

		return validateText(name);
	}

	@Override
	protected void renderForm() {
		TableWrapData td;
		Section section;
		Composite composite;
		GridData gd;
		
		/*
		 * Name
		 */
		section = toolkit.createSection(form.getBody(), Section.TITLE_BAR);
		td = new TableWrapData(TableWrapData.FILL);
		section.setLayoutData(td);
		section.setText("Название*");
		composite = toolkit.createComposite(section);
		composite.setLayout(new GridLayout(1, false));
		section.setClient(composite);
		gd = new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL);
		name = toolkit.createText(composite, "",
				SWT.BORDER);
		gd.widthHint = 100;
		name.setLayoutData(gd);
		name.addModifyListener(getNameModifyListener());
		
		/*
		 * Description
		 */
		section = toolkit.createSection(form.getBody(), Section.TITLE_BAR);
		td = new TableWrapData(TableWrapData.FILL_GRAB);
		section.setLayoutData(td);
		section.setText("Описание");
		composite = toolkit.createComposite(section);
		composite.setLayout(new GridLayout(1, false));
		section.setClient(composite);
		gd = new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL);
		description = toolkit.createText(composite, "",
				SWT.BORDER | SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);
		gd.widthHint = 200;
		gd.heightHint = 60;
		description.setLayoutData(gd);
		description.addModifyListener(getTextModifyListener());
		
		/*
		 * Values
		 */
		section = toolkit.createSection(form.getBody(), Section.TITLE_BAR
				| Section.TWISTIE | Section.EXPANDED);
		td = new TableWrapData(TableWrapData.FILL);
		td.heightHint = 300;
		section.setLayoutData(td);
		section.setText("Состав и содержание компонентов");
		composite = toolkit.createComposite(section);
		section.setClient(composite);
		valuesEditor = new FormulaTableEditor(composite, PersistenceManager.getInstance().getDataSource().getAll(EntityType.Component));
		valuesEditor.setInput(new ArrayList<Entity>());
		valuesEditor.addModifyListener(getEntityTableListener());

	}

}
